// 实现这个项目的构建任务
const sass = require('sass');
const loadGruntTasks = require('load-grunt-tasks');

const data = {
  menus: [{
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [{
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = (grunt) => {
  grunt.initConfig({
    sass: {
      options: {
        implementation: sass
      },
      dist: {
        files: [{
          expand: true,
          cwd: 'src/assets/styles',
          src: ['*.scss'],
          dest: 'dist/assets/styles',
          ext: '.css'
        }]
      }
    },
    babel: {
      options: {
        presets: [
          [
            '@babel/preset-env',
            {
              useBuiltIns: "usage",
              corejs: 3
            }
          ]
        ]
      },
      dist: {
        files: [{
          expand: true,
          cwd: 'src/assets/scripts',
          src: ['*.js'],
          dest: 'dist/assets/scripts'
        }]
      }
    },
    swigtemplates: {
      options: {
        templatesDir: 'src'
      },
      dist: {
        context: data,
        dest: 'dist/',
        src: ['src/*.html']
      }
    },
    imagemin: {
      images: {
        files: [{
          expand: true,
          cwd: 'src/assets/images',
          src: ['*.*'],
          dest: 'dist/assets/images'
        }]
      },
      fonts: {
        files: [{
          expand: true,
          cwd: 'src/assets/fonts',
          src: ['*.*'],
          dest: 'dist/assets/fonts'
        }]
      }
    },
    copy: {
      main: {
        files: [{
          expand: true,
          cwd: 'public/',
          src: ['*.*'],
          dest: 'dist'
        }]
      },
      usemin: {
        files: [{
          expand: true,
          cwd: 'dist/',
          src: ['*.html'],
          dest: 'dist'
        }]
      }
    },
    watch: {
      js: {
        files: ['src/assets/scripts/*.js'],
        tasks: ['babel']
      },
      css: {
        files: ['src/assets/styles/*.scss'],
        tasks: ['sass']
      },
      html: {
        files: ['src/**/*.html'],
        tasks: ['swigtemplates']
      }
    },
    clean: {
      dev: ['dist'],
      build: ['dist']
    },
    concurrent: {
      compile: ['sass', 'babel', 'swigtemplates']
    },
    browserSync: {
      dev: {
        bsFiles: {
          src: ['dist/assets/styles/*.css', 'dist/assets/scripts/*.js', 'dist/*.html']
        },
        options: {
          watchTask: true,
          server: {
            baseDir: "dist",
            routes: {
              '/node_modules': 'node_modules'
            }
          }
        }
      }
    },
    usemin: {
      html: 'dist/*.html',
      options: {
        assetsDirs: ['dist/']
      }
    },
    htmlmin: {
      dist: {
        options: {
          removeComments: true,
          collapseWhitespace: true
        },
        files: [{
          expand: true,
          cwd: 'dist',
          src: ['*.html'],
          dest: 'dist'
        }]
      },
    }
  })
  loadGruntTasks(grunt)

  grunt.registerTask('dev', ['compile', 'browserSync', 'watch'])
  grunt.registerTask('compile', ['clean:dev', 'concurrent:compile']);
  grunt.registerTask('build',['clean:build','compile','copy:usemin','usemin','htmlmin','copy:main','imagemin']);
}
